package com.whty.module.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.whty.common.annotation.Log;
import com.whty.common.base.AjaxResult;
import com.whty.common.enums.BusinessType;
import com.whty.framework.web.page.TableDataInfo;
import com.whty.mariotools.IdGenerator;
import com.whty.module.domain.DepotHead;
import com.whty.module.domain.DistLabel;
import com.whty.module.service.IDepotHeadService;
import com.whty.module.service.IDistLabelService;
import com.whty.system.domain.DepotHeadItem;
import com.whty.system.service.IDepotHeadItemService;
import com.whty.web.core.base.BaseController;

/**
 * 标签分配 信息操作处理
 * 
 * @author mario
 * @date 2018-11-02
 */
@Controller
@RequestMapping("/module/distLabel")
public class DistLabelController extends BaseController
{
    private String prefix = "module/distLabel";
	
	@Autowired
	private IDistLabelService distLabelService;
	@Autowired
	private IDepotHeadItemService depotHeadItemService;
	@Autowired
	private IDepotHeadService depotHeadService;
	
	@RequiresPermissions("module:distLabel:view")
	@GetMapping()
	public String distLabel()
	{
	    return prefix + "/distLabel";
	}
	
	/**
	 * 查询标签分配列表
	 */
	@RequiresPermissions("module:distLabel:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(DistLabel distLabel)
	{
		startPage();
        List<DistLabel> list = distLabelService.selectDistLabelList(distLabel);
		return getDataTable(list);
	}
	
	/**
	 * 新增标签分配
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存标签分配
	 */
	@RequiresPermissions("module:distLabel:add")
	@Log(title = "标签分配", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(DistLabel distLabel)
	{	
		distLabel.setDistNo(IdGenerator.getRanId18());
		distLabel.setDistTime(new Date());
		if(StringUtils.isNotBlank(distLabel.getBindingTagNo())){
			distLabel.setBindingMark("1");
			distLabel.setBindingTime(new Date());
		}else{
			distLabel.setBindingMark("0");
		}
		String depotHeadNo = distLabel.getDepotHeadNo();
		String beginSerialNumber = distLabel.getBeginSerialNumber();
		Integer distNum = distLabel.getDistNum();//分配数量
		DepotHeadItem depotHeadItem = new DepotHeadItem();
		List<String> snList = new ArrayList<String>();
		depotHeadItem.setDepotHeadNo(depotHeadNo);
		depotHeadItem.setAllocationStatus("00");
		List<DepotHeadItem> itemList = depotHeadItemService.selectDepotHeadItemList(depotHeadItem);
		if(CollectionUtils.isNotEmpty(itemList)){
			for (DepotHeadItem depotHeadItem2 : itemList) {
				snList.add(depotHeadItem2.getLabelSerialNumber());
			}
		}
		
		if(CollectionUtils.isNotEmpty(snList)){
			Collections.sort(snList);
			int index = 0;
			for (int i = 0; i < snList.size(); i++) {
				if(snList.get(i).equals(beginSerialNumber)){
					index = i;
					break;
				}
			}
			//更新入库明细
			for (int i = index; i < index+distNum; i++) {
				DepotHeadItem depotHeadItem3 = new DepotHeadItem();
				depotHeadItem3.setLabelSerialNumber(snList.get(i));
				List<DepotHeadItem> depotHeadItemList = depotHeadItemService.selectDepotHeadItemList(depotHeadItem3);
				if(CollectionUtils.isNotEmpty(depotHeadItemList)){
					DepotHeadItem depotHeadItem4 = depotHeadItemList.get(0);
					depotHeadItem4.setAllocationStatus("01");
					depotHeadItem4.setAllocationTime(new Date());
					depotHeadItem4.setUpdateTime(new Date());
					depotHeadItem4.setGardenNo(distLabel.getGardenNo());
					depotHeadItem4.setAllocationBatch(distLabel.getDistNo());
					depotHeadItemService.updateDepotHeadItem(depotHeadItem4);
				}
			}
			
			//更新入库单
			DepotHead depotHead = new DepotHead();
			depotHead.setDepotHeadNo(depotHeadNo);
			List<DepotHead> depotHeadList = depotHeadService.selectDepotHeadList(depotHead);
			if(CollectionUtils.isNotEmpty(depotHeadList)){
				DepotHead depotHead2 = depotHeadList.get(0);
				depotHead2.setCurrentSerialNumber(snList.get(index+distNum-1));//当前序列号
				if(depotHead2.getStatus().equals("00")){//如果是未使用状态
					if(distNum < depotHead2.getAmount()){
						depotHead2.setStatus("01");	//已使用
					}else if(distNum == depotHead2.getAmount()){
						depotHead2.setStatus("02");	//使用完
					}
					depotHead2.setUsedAmount(distNum);
					depotHead2.setUnusedAmount(depotHead2.getAmount()-distNum);
				}else if(depotHead2.getStatus().equals("01")){//如果是已使用的状态
					Integer usedAmount = depotHead2.getUsedAmount();//已使用数量
					if((distNum+usedAmount) < depotHead2.getAmount()){
						depotHead2.setStatus("01");	//已使用	
					}else if((distNum+usedAmount) == depotHead2.getAmount()){
						depotHead2.setStatus("02");	//使用完
					}
					depotHead2.setUsedAmount(distNum+usedAmount);
					depotHead2.setUnusedAmount(depotHead2.getUnusedAmount()-distNum);
				}
				depotHead2.setUpdateTime(new Date());
				depotHeadService.updateDepotHead(depotHead2);
			}
		}
		
		return toAjax(distLabelService.insertDistLabel(distLabel));
	}

	/**
	 * 修改标签分配
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		DistLabel distLabel = distLabelService.selectDistLabelById(id);
		mmap.put("distLabel", distLabel);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存标签分配
	 */
	@RequiresPermissions("module:distLabel:edit")
	@Log(title = "标签分配", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(DistLabel distLabel)
	{	
		String bindingTagNo = distLabel.getBindingTagNo();
		System.out.println(bindingTagNo);
		if(StringUtils.isNotBlank(bindingTagNo)){
			distLabel.setBindingMark("1");
			distLabel.setBindingTime(new Date());
		}else{
			distLabel.setBindingTagNo("");
			distLabel.setBindingMark("0");
			distLabel.setBindingTime(null);
		}
		distLabel.setUpdateDate(new Date());
		return toAjax(distLabelService.updateDistLabel(distLabel));
	}
	
	/**
	 * 删除标签分配
	 */
	@RequiresPermissions("module:distLabel:remove")
	@Log(title = "标签分配", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(distLabelService.deleteDistLabelByIds(ids));
	}
	
}
